/*
 * @Author: TC (tengchao@taqu.cn)
 * @Date: 2025-09-18 16:40:33
 * @Last Modified by: TC (tengchao@taqu.cn)
 * @Last Modified time: 2025-10-17 16:38:51
 */

import { Node } from "cc";
import { MoleHole } from "./Item/MoleHole";
import { app } from "db://assets/FrameWork/App";
import { nsMoleMissionCfg } from "../../Config/MoleMisisonCfg";
import { EMoleEventConst } from "../../Const/MoleEventConst";
import { EMoleUIConst } from "../../Const/MoleUIConst";
import { gameMole } from "../../GameMole";

/**
 * 打地鼠战斗管理
 */
export class MoleBattleManager {
    //鼠洞列表
    private _holeList: MoleHole[] = [];
    //鼠洞层
    public layerHole: Node = null;

    private _createTimer = -1;
    private _missionId = 0;

    private _tickList: any[] = [];
    //是否暂停
    private _pause: boolean = false;
    /**
     * 杀死地鼠数量
     */
    private _killNum = 0;
    public get killNum(): number {
        return this._killNum;
    }
    /**
     * 当前关卡配置
     */
    private _missionCfg: IMoleCfgInterface.IMission = null;
    public get missionCfg(): IMoleCfgInterface.IMission {
        return this._missionCfg;
    }

    constructor() {}

    public addHole(hole: MoleHole): void {
        this._holeList.push(hole);
    }

    /**
     * 开始战斗
     */
    public startBattle(): void {
        this.nextMission();
        const interval = this._missionCfg.interval || 0.5;
        this._createTimer = app.timerMgr.schedule(this._onCreateMole, this, null, interval, -1, 0);
    }

    /**
     * 创建地鼠回调
     */
    private _onCreateMole(): void {
        const hole = this._getValidHole();
        if (hole) {
            if (hole.createMole()) {
            }
        }
    }

    /**
     * 获取一个空的地鼠洞
     * @returns
     */
    private _getValidHole(): MoleHole {
        for (let i = 0; i < 50; i++) {
            const rad = Math.floor(Math.random() * this._holeList.length);
            if (this._holeList[rad].isEmpty()) {
                return this._holeList[rad];
            }
        }
        return null;
    }

    /**
     * 结束战斗
     */
    public endBattle(): void {
        app.uiMgr.openUI(EMoleUIConst.ResultUI, null, () => {
            this.endBattle();
        });
    }

    /**
     * 直接退出战斗
     */
    public exitBattle(): void {
        this.release();
        app.uiMgr.closeUI(EMoleUIConst.BattleUI);
    }

    /**
     * 杀死地鼠
     */
    public killMole(): void {
        this._killNum++;
        // app.log.info(`杀死地鼠: ${this._killNum}`);
        //达到下一关条件
        if (this._killNum >= this._missionCfg.lvlUpNum) {
            this.nextMission();
        }
    }

    /**
     * 地鼠跑开
     */
    public moleRunAway(): void {
        app.eventMgr.dispatch(EMoleEventConst.ReduceHp, 0.25);
    }

    /**
     * 下一关卡
     */
    public nextMission(): void {
        this._killNum = 0;
        this._missionId++;
        this._missionId = Math.min(this._missionId, nsMoleMissionCfg.getMaxMissionId());
        app.log.info(`进入新关卡: ${this._missionId}`);
        this._missionCfg = nsMoleMissionCfg.getMissionCfg(this._missionId);
        if (this._missionId > 1) {
            //升级动画
        }
    }

    /**
     * 暂停战斗
     */
    public pause(): void {
        this._pause = true;
        app.timerMgr.pause(this._createTimer);
        this._holeList.forEach((hole) => {
            hole.pause();
        });
        app.uiMgr.openUI(EMoleUIConst.PauseUI);

        app.eventMgr.dispatch(EMoleEventConst.PauseBattle);
    }

    /**
     * 继续战斗
     */
    public resume(): void {
        this._pause = false;
        app.timerMgr.resume(this._createTimer);
        this._holeList.forEach((hole) => {
            hole.resume();
        });
        app.eventMgr.dispatch(EMoleEventConst.ResumeBattle);
    }

    public tick(dt: number): void {
        if (this._pause) {
            return;
        }
        this._tickList.forEach((item) => {
            item.tick?.(dt);
        });
    }
    /**
     * 释放
     */
    public release(): void {
        app.timerMgr.unschedule(this._createTimer);
        this._holeList.forEach((hole) => {
            hole.release();
        });
        this._holeList.length = 0;
        this._missionId = 0;
        this._killNum = 0;
        this._missionCfg = null;
        this._pause = false;
        gameMole.capMgr.release();
    }
}
